//
//  VisitorView.swift
//  Microblog
//
//  Created by steaest on 15/7/30.
//  Copyright © 2015年 steaest. All rights reserved.
//

//1.override init(frame: CGRect)
//  required init?(coder aDecoder: NSCoder) 区别

//2.required init?(coder aDecoder: NSCoder) {
//  禁止用 sb / xib 使用本类
//  fatalError("init(coder:) has not been implemented") 前后两句是什么意思?
//  super.init(coder: aDecoder)
//  setupUI()
//  }

//3.relatedBy: NSLayoutRelation.Equal 自动布局里面这个参数的意思

//4.delegate?.visitorViewWillRegister()设置按钮的代理方法的时候这句话的意思

import UIKit

// MARK:访客视图协议
protocol VisitorViewDelegate: NSObjectProtocol {
    
    func visitorViewWillRegister()
    func visitorViewWillLogin()
}

// MARK:访客视图
class VisitorView: UIView {
    
    //设置代理属性
    weak var delegate: VisitorViewDelegate?

    //按钮方法监听
    func clickRegister() {
        delegate?.visitorViewWillRegister()
    }
    func clickLogin() {
        delegate?.visitorViewWillLogin()
    }
    
    // MARK: 设置视图信息
    func setupViewInfo(isHome: Bool, imageName: String, message: String) {
        messageLabel.text = message
        iconView.image = UIImage(named: imageName)
        
        homeView.hidden = !isHome
        
        isHome ? startAnimation() : sendSubviewToBack(maskIconView)
    
    }
    
    // MARK:设置的动画
    private func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = M_PI * 2
        anim.repeatCount = MAXFLOAT
        anim.duration = 20.0
        
        //设置动画不被移除
        anim.removedOnCompletion = false
    
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    
    // MARK: 界面初始化
    
    //纯代码时使用这个方法初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    //使用SB或者XIB用该方法初始化
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        setupUI()
    }
    
    //MARK: 设置界面
    private func setupUI() {
        // 1.添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 2.自动布局
        
        //图片
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -60))
        
        //遮罩
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        //小房子
        homeView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        //文字
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 224))
        
        //注册按钮
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 85))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Height, multiplier: 1.0, constant: 35))
        
        //登录按钮
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 85))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Height, multiplier: 1.0, constant: 35))
        
        //遮罩
        
        
        //背景颜色
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
    
    }
    
    //MARK:懒加载控件
    
    //图标
    private lazy var iconView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return iv
    }()
    
    //遮罩
    private lazy var maskIconView: UIImageView = {
        let mv = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return mv
    }()
    
    //小房子
    private lazy var homeView: UIImageView = {
        let hv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return hv
    }()
    
    //文字
    private lazy var messageLabel: UILabel = {
        let ml = UILabel()
        
        ml.text = "关注一些人，回这里看看有什么惊喜关注一些人"
        ml.textColor = UIColor.darkGrayColor()
        ml.numberOfLines = 0
        ml.font = UIFont.systemFontOfSize(14)
        
        return ml
    }()
    
    //注册按钮
    private lazy var registerButton: UIButton = {
        let btn = UIButton()
        
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "clickRegister", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
    //登录按钮
    private lazy var loginButton: UIButton = {
        let btn = UIButton()
        
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "clickLogin", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    

}
